package com.lry.web.fiction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lry.web.config.RabbitMQConfig;
import com.lry.web.fiction.entity.Fiction;
import com.lry.web.fiction.mapper.FictionMapper;
import com.lry.web.fiction.service.FictionChapterService;
import com.lry.web.fiction.service.FictionContentService;
import com.lry.web.fiction.service.FictionService;
import com.lry.web.fiction.service.FictionUpdateService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FictionServiceImpl implements FictionService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    FictionMapper fictionMapper;

    @Autowired
    FictionChapterService fictionChapterService;
    @Autowired
    FictionContentService fictionContentService;
    @Autowired
    FictionUpdateService fictionUpdateService;




    //根据条件在mysql查询小说
    public Map<String,Object> getFictions(String key, String option, Integer from, Integer size) throws IOException {
        Page<Fiction> page = new Page<Fiction>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        if("title".equals(option)){
            wrapper.like("title",key);
            wrapper.orderByAsc("length(title)");
        }else{
            wrapper.like(option,key);
            wrapper.orderByDesc("update_time");
        }
        fictionMapper.selectPage(page,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("data",page.getRecords());
        map.put("count",page.getTotal());
        return map;
    }


    //根据FVId删除小说,效率非常低,不推荐使用
    public int deleteByFVId(String fVId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fiction_variable_id",fVId);
        fictionChapterService.deleteByFVId(fVId);
        fictionContentService.deleteByFVId(fVId);
        fictionUpdateService.deleteByFVId(fVId);

        return fictionMapper.delete(wrapper);
    }



    //发起采集请求到MQ
    public int spiderFiction(String title) throws InterruptedException {
        rabbitTemplate.convertAndSend(RabbitMQConfig.GATHEREXCHANGE_NAME,"fiction.gather",title);
        Thread.sleep(1000);
        while(RabbitMQConfig.code==0){
            Thread.sleep(1000);
        }
        int i = RabbitMQConfig.code;
        RabbitMQConfig.code = 0;
        return i;
    }
    //获取小说总数量
    public Integer selectNum() {
        return fictionMapper.selectNum();
    }

    //获取全部小说，分页
    public Map<String,Object> selectFictions(int from, int size,String option, String key) {
        Page<Fiction> page = new Page<Fiction>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("update_time");
        if(option !=null && key != null){
            wrapper.like(option,key);
        }
        fictionMapper.selectPage(page, wrapper);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("fictions",page.getRecords());
        map.put("pages",page.getPages());
        return map;
    }



    //更具FId删除小说
    public int deleteByFictionId(String fictionId) throws Exception {

        List<String> list=  Arrays.asList(fictionId.split(","));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("fiction_id",list);
        fictionChapterService.deleteByFictionId(fictionId);
        fictionContentService.deleteByFictionId(fictionId);
        fictionUpdateService.deleteByFictionId(fictionId);

        return fictionMapper.delete(wrapper);
    }



}
